package com.zhijian.medical.service.impl;

import cn.hutool.core.date.DateUtil;
import com.zhijian.medical.dao.local.RoutinePathologyTsSjMapper;
import com.zhijian.medical.entity.po.RoutinePathologyTsSj;
import com.zhijian.medical.entity.vo.request.RoutinePathologyTsSjSearchVo;
import com.zhijian.medical.entity.vo.request.RoutinePathologyTsSjSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.RoutinePathologyTsSjVo;
import com.zhijian.medical.service.RoutinePathologyTsSjService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 常规病例-脱水-试剂列表业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-07-25 20:04:33
 */
@Slf4j
@Service
public class RoutinePathologyTsSjServiceImpl implements RoutinePathologyTsSjService {

	@Autowired
    private RoutinePathologyTsSjMapper mapper;

    @Override
    public RoutinePathologyTsSj selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public RoutinePathologyTsSjVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(RoutinePathologyTsSj routinePathologyTsSj) {
        Date now = new Date();
        routinePathologyTsSj.setCreateTime(now);
        routinePathologyTsSj.setUpdateTime(now);
        return mapper.insert(routinePathologyTsSj) > 0 ? routinePathologyTsSj.getId() : 0L;
    }

    @Override
    public Long insertSelective(RoutinePathologyTsSj routinePathologyTsSj) {
        Date now = new Date();
        routinePathologyTsSj.setCreateTime(now);
        routinePathologyTsSj.setUpdateTime(now);
        return mapper.insertSelective(routinePathologyTsSj) > 0 ? routinePathologyTsSj.getId() : 0L;
    }

    @Override
    public boolean update(RoutinePathologyTsSj routinePathologyTsSj) {
        Date now = new Date();
        routinePathologyTsSj.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(routinePathologyTsSj) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(RoutinePathologyTsSj record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(RoutinePathologyTsSjSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologyTsSj routinePathologyTsSj = new RoutinePathologyTsSj();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologyTsSj);
        return this.insert(routinePathologyTsSj) != 0;
    }

    @Override
    public boolean modify(RoutinePathologyTsSjSaveOrUpdateVo saveOrUpdateVo) {
        RoutinePathologyTsSj old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        RoutinePathologyTsSj routinePathologyTsSj = new RoutinePathologyTsSj();
        BeanUtils.copyProperties(saveOrUpdateVo, routinePathologyTsSj);
        routinePathologyTsSj.setLastTime(DateUtil.parseDateTime(saveOrUpdateVo.getLastTimeStr()));
        routinePathologyTsSj.setNextTime(DateUtil.parseDateTime(saveOrUpdateVo.getNextTimeStr()));
        return this.update(routinePathologyTsSj);
    }

    @Override
    public RoutinePathologyTsSjVo selectOne(RoutinePathologyTsSjSearchVo routinePathologyTsSjSearchVo) {
        return mapper.selectOne(routinePathologyTsSjSearchVo);
     }

    @Override
    public Integer count(RoutinePathologyTsSjSearchVo routinePathologyTsSjSearchVo) {
        return mapper.count(routinePathologyTsSjSearchVo);
    }

    @Override
    public List<RoutinePathologyTsSjVo> selectList(RoutinePathologyTsSjSearchVo routinePathologyTsSjSearchVo) {
        return mapper.selectList(routinePathologyTsSjSearchVo);
    }

    @Override
    public Page<RoutinePathologyTsSjVo> selectPage(RoutinePathologyTsSjSearchVo routinePathologyTsSjSearchVo) {
        AssertUtil.check(Objects.isNull(routinePathologyTsSjSearchVo.getPageNum()) || Objects.isNull(routinePathologyTsSjSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(routinePathologyTsSjSearchVo);
        //分页
        Page<RoutinePathologyTsSjVo> page = new Page<>(true, count, routinePathologyTsSjSearchVo.getPageNum(), routinePathologyTsSjSearchVo.getPageSize());
        routinePathologyTsSjSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<RoutinePathologyTsSjVo> responseVos = this.selectList(routinePathologyTsSjSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<RoutinePathologyTsSj> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(RoutinePathologyTsSjSearchVo routinePathologyTsSjSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(routinePathologyTsSjSearchVo);
    }
    
}